package com.jobsika.client.header;

import com.google.gwt.event.dom.client.BlurEvent;
import com.google.gwt.event.dom.client.BlurHandler;
import com.google.gwt.event.dom.client.ClickEvent;
import com.google.gwt.event.dom.client.ClickHandler;
import com.google.gwt.event.dom.client.FocusEvent;
import com.google.gwt.event.dom.client.FocusHandler;
import com.google.gwt.event.dom.client.KeyPressEvent;
import com.google.gwt.event.dom.client.KeyPressHandler;
import com.google.gwt.event.dom.client.MouseOutEvent;
import com.google.gwt.event.dom.client.MouseOutHandler;
import com.google.gwt.event.dom.client.MouseOverEvent;
import com.google.gwt.event.dom.client.MouseOverHandler;
import com.google.gwt.user.client.History;
import com.google.gwt.user.client.Window;
import com.google.gwt.user.client.ui.Button;
import com.google.gwt.user.client.ui.Composite;
import com.google.gwt.user.client.ui.HasHorizontalAlignment;
import com.google.gwt.user.client.ui.HorizontalPanel;
import com.google.gwt.user.client.ui.TextBox;
import com.jobsika.client.Util;
import com.jobsika.client.navigation.Logo;

public class Header extends Composite {
	private HorizontalPanel skeleton;
	private HorizontalPanel subskeleton;
	private HorizontalPanel lskeleton;
	private HorizontalPanel rskeleton;
	private Logo logo;
	public Logo login;
	private Logo logout;
	private Logo register;
	private Logo settings;
	private Logo sep0;
	private Logo search;
	private Logo home;
	private Logo connections;
	private Button searchButton;
	private TextBox searchbox;
	private String loginURL;
	private String logoutURL;
	private boolean loggedIn;
	
	public Header() {
		skeleton = new HorizontalPanel();
	    subskeleton = new HorizontalPanel();
		lskeleton = new HorizontalPanel();
		rskeleton = new HorizontalPanel();
		loginURL = "";
		loggedIn = false;
		
	    logo = new Logo();
	    login = new Logo();
	    logout = new Logo();
	    register = new Logo();
	    settings = new Logo();
	    sep0 = new Logo();
	    home = new Logo();
	    connections = new Logo();
	    search = new Logo();
	    searchButton = new Button("Search");
	    searchbox = new TextBox();
	    
	    // Buttons
	    logo.addMouseOverHandler(logoOver);
		logo.addClickHandler(logoClick);
		logo.addMouseOutHandler(logoOut);
		
		login.addMouseOverHandler(loginOver);
		login.addClickHandler(loginClick);
		login.addMouseOutHandler(loginOut);

		logout.addMouseOverHandler(logoutOver);
		logout.addClickHandler(logoutClick);
		logout.addMouseOutHandler(logoutOut);
	    
		settings.addMouseOverHandler(settingsOver);
		settings.addClickHandler(settingsClick);
		settings.addMouseOutHandler(settingsOut);
		
		home.addMouseOverHandler(homeOver);
		home.addClickHandler(homeClick);
		home.addMouseOutHandler(homeOut);
		
		connections.addMouseOverHandler(connOver);
		connections.addClickHandler(connClick);
		connections.addMouseOutHandler(connOut);
		
		search.addMouseOverHandler(searchOver);
		search.addClickHandler(searchClick);
		search.addMouseOutHandler(searchOut);
		
		
		// Textbox
		searchbox.addKeyPressHandler(keyPress);
		searchbox.addFocusHandler(keyFocus);
		searchbox.addBlurHandler(keyBlur);

	    skeleton.setStyleName("header");
	    subskeleton.setStyleName("header-panel");
	    lskeleton.setStyleName("header-left");
	    rskeleton.setStyleName("header-right");
	    logo.addStyleName("header-logo");
	    register.addStyleName("header-register");
		login.addStyleName("header-login");
		logout.addStyleName("header-logout");
		settings.addStyleName("header-settings");
		sep0.setStyleName("header-separator");
		sep0.addStyleName("header-sep0");
		home.addStyleName("header-home");
		connections.addStyleName("header-conn");
	    search.addStyleName("header-search");
	    searchbox.setStyleName("header-searchbox");
	    searchButton.addStyleName("header-searchbutton");
	    
	    lskeleton.add(logo);
		subskeleton.add(lskeleton);
	    subskeleton.add(rskeleton);
	    
	    subskeleton.setCellHorizontalAlignment(rskeleton, HasHorizontalAlignment.ALIGN_RIGHT);
	    subskeleton.setCellHorizontalAlignment(lskeleton, HasHorizontalAlignment.ALIGN_LEFT);
	    lskeleton.setHorizontalAlignment(HasHorizontalAlignment.ALIGN_LEFT);
	    rskeleton.setHorizontalAlignment(HasHorizontalAlignment.ALIGN_RIGHT);
	    skeleton.add(subskeleton);
	    
	    this.initWidget(skeleton);
	    setLogin();
	    idleSearchBox();
	}
	
	
	private void setLogin() {
		rskeleton.clear();
	    rskeleton.add(login);
	    lskeleton.clear();
	    lskeleton.add(logo);
	}
	
	private void setLogout() {		
		rskeleton.clear();
		rskeleton.add(settings);
		rskeleton.add(logout);
	    lskeleton.add(home);
	    lskeleton.add(connections);
	    lskeleton.add(search);
	    lskeleton.add(searchbox);
	}
	
	public void setLoggedIn(boolean loggedIn) {
		this.loggedIn = loggedIn;
		if (this.loggedIn) {
			setLogout();
		} else {
			setLogin();
		}
	}
	
	public void setLoginURL(String loginURL) {
		this.loginURL = loginURL;
	}
	
	public void setLogoutURL(String logoutURL) {
		this.logoutURL = logoutURL;
	}
	
	// Handlers
	private KeyPressHandler keyPress = new KeyPressHandler() {
		@Override
		public void onKeyPress(KeyPressEvent event) {
			int charcode = event.getCharCode();
			if (charcode == 13) {
				if (searchbox.getText().length() > 0) {
					History.newItem("q="+searchbox.getText()+"&pg=0");
					History.fireCurrentHistoryState();
					idleSearchBox();
				}
			}
		}
	};
	
	private FocusHandler keyFocus = new FocusHandler() {
		@Override
		public void onFocus(FocusEvent event) {
			if (searchbox.getText().equals(Util.searchtext)) {
				searchbox.setText("");
				searchbox.removeStyleName("header-searchbox-idle");
			}
		}
	};
	
	private BlurHandler keyBlur = new BlurHandler() {
		@Override
		public void onBlur(BlurEvent event) {
			if (searchbox.getText().length() == 0) {
				idleSearchBox();
			}
		}
	};
	
	private void idleSearchBox() {
		searchbox.setText(Util.searchtext);
		searchbox.addStyleName("header-searchbox-idle");
	}
	
	private MouseOverHandler loginOver = new MouseOverHandler() {
		public void onMouseOver(MouseOverEvent event) {
			login.addStyleName("header-login-over");
		}
	};
	
	private MouseOutHandler loginOut = new MouseOutHandler() {
		public void onMouseOut(MouseOutEvent event) {
			login.removeStyleName("header-login-over");
		}
	};
	
	private MouseOverHandler logoOver = new MouseOverHandler() {
		public void onMouseOver(MouseOverEvent event) {
			logo.addStyleName("header-logo-over");
		}
	};
	
	private MouseOutHandler logoOut = new MouseOutHandler() {
		public void onMouseOut(MouseOutEvent event) {
			logo.removeStyleName("header-logo-over");
		}
	};
	
	private MouseOverHandler logoutOver = new MouseOverHandler() {
		public void onMouseOver(MouseOverEvent event) {
			logout.addStyleName("header-logout-over");
		}
	};
	
	private MouseOutHandler logoutOut = new MouseOutHandler() {
		public void onMouseOut(MouseOutEvent event) {
			logout.removeStyleName("header-logout-over");
		}
	};
	
	private MouseOverHandler settingsOver = new MouseOverHandler() {
		public void onMouseOver(MouseOverEvent event) {
			settings.addStyleName("header-settings-over");
		}
	};
	
	private MouseOutHandler settingsOut = new MouseOutHandler() {
		public void onMouseOut(MouseOutEvent event) {
			settings.removeStyleName("header-settings-over");
		}
	};
	
	private MouseOverHandler homeOver = new MouseOverHandler() {
		public void onMouseOver(MouseOverEvent event) {
			home.addStyleName("header-home-over");
		}
	};
	
	private MouseOutHandler homeOut = new MouseOutHandler() {
		public void onMouseOut(MouseOutEvent event) {
			home.removeStyleName("header-home-over");
		}
	};
	
	private MouseOverHandler connOver = new MouseOverHandler() {
		public void onMouseOver(MouseOverEvent event) {
			connections.addStyleName("header-conn-over");
		}
	};
	
	private MouseOutHandler connOut = new MouseOutHandler() {
		public void onMouseOut(MouseOutEvent event) {
			connections.removeStyleName("header-conn-over");
		}
	};
	
	private MouseOverHandler searchOver = new MouseOverHandler() {
		public void onMouseOver(MouseOverEvent event) {
			search.addStyleName("header-search-over");
		}
	};
	
	private MouseOutHandler searchOut = new MouseOutHandler() {
		public void onMouseOut(MouseOutEvent event) {
			search.removeStyleName("header-search-over");
		}
	};
	
	private ClickHandler homeClick = new ClickHandler() {
		@Override
		public void onClick(ClickEvent event) {
			History.newItem(Util.HOME);
			History.fireCurrentHistoryState();
		}
	};
	
	private ClickHandler searchClick = new ClickHandler() {
		@Override
		public void onClick(ClickEvent event) {
			History.newItem(Util.QUERY+"="+Util.POST);
			History.fireCurrentHistoryState();
		}
	};
	
	private ClickHandler connClick = new ClickHandler() {
		@Override
		public void onClick(ClickEvent event) {
			History.newItem(Util.QUERY+"="+Util.USER);
			History.fireCurrentHistoryState();
		}
    };
    
    private ClickHandler logoClick = new ClickHandler() {
		@Override
		public void onClick(ClickEvent event) {
			History.newItem(Util.HOME);
			History.fireCurrentHistoryState();
		}
	};
	
	private ClickHandler settingsClick = new ClickHandler() {
		@Override
		public void onClick(ClickEvent event) {
			Window.alert("Settings");
		}
	};
	
	private ClickHandler loginClick = new ClickHandler() {
		@Override
		public void onClick(ClickEvent event) {
			Window.Location.assign(loginURL);
		}
	};
	
	private ClickHandler logoutClick = new ClickHandler() {
		@Override
		public void onClick(ClickEvent event) {
			Window.Location.assign(logoutURL);
		}
	};
}
